package stendcontrol;

import java.io.IOException;
import services.ConvIntByte;

public class UdpImplementer {

    /**
     * Associations
     */
    private UdpServer udp_serv;
    private int id = 0;

    public UdpImplementer(UdpServer udp_serv) {
        this.udp_serv = udp_serv;
    }

    private int getId() {
        return ++id < 16 ? id : (id = 0);
    }

    /**
     * Operation
     *
     * @param cm
     * @return
     */
    public void sendPacket(CommandMessage cm) {
        
        cm.setId(getId());
       /* System.out.println("CM data :  " + Integer.toHexString(ConvIntByte.byte2Int(cm.getBytes()[0]))
                + "  ||  " + Integer.toHexString(ConvIntByte.byte2Int(cm.getBytes()[1])));*/
        byte[] msg = cm.getBytes();
        try {
            udp_serv.sendPacket(msg, msg.length);
        } catch (IOException ex) {
            StendLogger.log(StendLogger.MSG_ERROR, "Error in sendPacket:"
                    + ex.getMessage());
        }
    }
}
